package mi.payment.transaction.sale;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Configs;
import mi.payment.constants.Params;
import mi.payment.controller.BaseController;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.PageRoute;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.transaction.QRCodeQueryTask;
import sunmi.payment.ui.dialog.LoadDialog;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 二维码交易查询
 */
public class QRCodeQueryController extends BaseController {

    private String mReqVoucherNo;
    private String mReqTradeTime;
    private String mReqTradeDate;

    private PayDetail mReqPayDetail;

    public interface ResultCallback {

        void queryComplete(PayDetail payDetail);

        void queryTimeout();

    }

    private ResultCallback mCallback;

    public QRCodeQueryController(ParentActivity activity, @NonNull PayDetail payDetail, ResultCallback callback) {
        super(activity);
        mCallback = callback;
        mReqPayDetail = payDetail;
    }

    public QRCodeQueryController(ParentActivity activity) {
        super(activity);
    }

    public void queryLastQRCodeTrade() {
        if (mReqPayDetail == null) {
            mReqPayDetail = PayDetailModel.getInstance().getLastQRCodeConsumptionTrade();
        }
        if (mReqPayDetail == null) {
            activity.showToast(R.string.error_last_transaction);
            return;
        }

        mReqVoucherNo = mReqPayDetail.getVoucherNo();
        TradeData tradeData = Params.newTradeData();
        PayDetail newPayDetail = tradeData.getPayDetail();
        String newVoucherNo = newPayDetail.getVoucherNo();
        LogUtil.e(Configs.TAG, "originalVoucherNo：" + mReqVoucherNo + " newVoucherNo：" + newVoucherNo);

        mReqPayDetail.setOriginalPOSNum(mReqVoucherNo);
        mReqPayDetail.setVoucherNo(newVoucherNo);
        mReqPayDetail.setTradeResultDes("");
        mReqPayDetail.setQRCodeTransState(0);
        mReqPayDetail.setTradeAnswerCode("96");
        mReqTradeTime = mReqPayDetail.getTradeTime();
        mReqTradeDate = mReqPayDetail.getTradeDate();

        tradeData.setPayDetail(mReqPayDetail);

        @SuppressLint("StaticFieldLeak")
        QRCodeQueryTask task = new QRCodeQueryTask(activity) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();

                // 如果回调不为空 调用回调 后续不做处理 by Ricardo start
                if (mCallback != null) {
                    mCallback.queryComplete(resPay);
                    return;
                }

                // 查询后更新数据库中的原订单的参考号和订单号
                mReqPayDetail.setThirdTransNum(resPay.thirdTransNum);
                mReqPayDetail.setTradeResultDes(resPay.tradeResultDes);
                mReqPayDetail.setTradeAnswerCode(resPay.tradeAnswerCode);
                mReqPayDetail.setQRCodeTransState(resPay.QRCodeTransState);
                if (mReqPayDetail.referNo == null || mReqPayDetail.referNo.length() == 0) {
                    mReqPayDetail.setReferNo(resPay.referNo);
                }
                // 多码合一的时候一定要重置交易平台，否则打印小票有问题
                mReqPayDetail.setTransPlatform(resPay.transPlatform);
                // 保存原交易的 流水号 日期 时间
                mReqPayDetail.setVoucherNo(mReqVoucherNo);
                mReqPayDetail.setTradeDate(mReqTradeDate);
                mReqPayDetail.setTradeTime(mReqTradeTime);

                // 末笔查询不管成功失败都更新数据
                PayDetailModel.getInstance().update(mReqPayDetail);

                PageRoute.startAction(mActivity, PageRoute.TYPE_ORDER_QUERY, mReqPayDetail);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                if (mCallback != null) {
                    mCallback.queryTimeout();
                } else {
                    PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_ORDER_QUERY, mReqPayDetail);
                }
            }

        };
        LoadDialog dialog = mActivity.getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(dialog);
        task.execute(tradeData);
    }


}
